package com.shy.controller;

import com.github.pagehelper.PageInfo;
import com.shy.config.model.R;
import com.shy.entity.Clue;
import com.shy.entity.ClueRemark;
import com.shy.service.ClueService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 线索管理
 */
@RestController
@RequestMapping("/api/clue")
public class ClueController {
    @Resource
    private ClueService clueService;

    @GetMapping("/pageList")
    public R<PageInfo<Clue>> pageList(@RequestParam Integer current,
                                      @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        PageInfo<Clue> clueList = clueService.queryClueList(current, pageSize);
        return R.success(clueList);
    }

    @ApiOperation("获取线索详情")
    @GetMapping("/detail/{clueId}")
    public R<Clue> detail(@PathVariable Integer clueId) {
        Clue clue = clueService.queryClueById(clueId);
        return R.success(clue);
    }

    @ApiOperation("查询线索备注")
    @GetMapping("/remarkList/{clueId}")
    public R<List<Clue>> remarkList(@PathVariable Integer clueId) {
        List<Clue> clueList = clueService.queryClueByIds(clueId);
        return R.success(clueList);
    }

    @ApiOperation("线索导入")
    @PostMapping("/importExcel")
    public R<String> importClue(MultipartFile file) throws IOException {
        clueService.batchImportClue(file);
        return R.success(null);
    }

    @ApiOperation("添加线索")
    @PostMapping("/addClue")
    public R<String> saveClue(@RequestBody Clue clue) {
        Boolean saved = clueService.saveClue(clue);
        return R.HANDLE(saved);
    }

    @ApiOperation("编辑线索")
//    @PreAuthorize("hasAuthority('xxx')")
    @PutMapping("/editClue")
    public R<String> modifyClue(@ApiParam("线索对象") @RequestBody Clue clue) {
        Boolean modified = clueService.modifyClue(clue);
        return R.HANDLE(modified);
    }

    @ApiOperation("批量删除线索")
//    @PreAuthorize("hasAuthority('xxx')")
    @DeleteMapping("batch/{ids}")
    public R<String> batchRemoveClues(@ApiParam("线索id集合") @PathVariable List<Integer> ids) {
        Boolean removed = clueService.batchRemoveCluesByIds(ids);
        return R.HANDLE(removed);
    }

    @ApiOperation("添加线索备注")
    @PostMapping("/addRemark")
    public R<String> addRemark(@RequestBody ClueRemark clueRemark) {
        // 添加活动备注
        Boolean clueRemarks = clueService.addActivityRemarks(clueRemark);
        return clueRemarks ? R.success(null) : R.failure("添加活动备注失败");
    }

    @ApiOperation("删除线索备注")
    @DeleteMapping("/deleteRemark/{clueRemarkId}") 
    public R<String> deleteRemark(@PathVariable Integer clueRemarkId) {
        // 删除活动备注
        Boolean clueRemarks = clueService.deleteRemark(clueRemarkId);
        return R.HANDLE(clueRemarks);
    }
    
}
